package com.ruoyi.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.pojo.Colllect;
import com.ruoyi.pojo.Product;
import com.ruoyi.service.ColllectService;
import com.ruoyi.service.ProductService;
import com.ruoyi.utils.R;
import com.ruoyi.vo.ProductDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/collect")
@CrossOrigin
public class CollectController {

    @Autowired
    private ColllectService colllectService;
    @Autowired
    private ProductService productService;

    @PostMapping("/add")
    public R<String> add(@RequestBody Colllect colllect){
        int i = colllectService.addCollect(colllect);
        if (i > 0) {
            return R.success("添加成功");
        }else{
            return R.error("添加失败");
        }
    }

    @DeleteMapping("/remove")
    public R<String> remove(@RequestBody Colllect colllect){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("userid",colllect.getUserid());
        wrapper.eq("productid",colllect.getProductid());
        boolean remove = colllectService.remove(wrapper);
        if (remove) {
            return R.success("删除成功");
        }else{
            return R.error("删除失败");
        }
    }

    @GetMapping("/one")
    public R<Colllect> one(Colllect colllect) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("userid",colllect.getUserid());
        wrapper.eq("productid",colllect.getProductid());
        Colllect one = colllectService.getOne(wrapper);
        return R.success(one);
    }

    @GetMapping("/list")
    public R<List<ProductDto>> list(@RequestParam Integer userid){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("userid",userid);
        List<Colllect> list = colllectService.list(wrapper);
        ArrayList<ProductDto> products = new ArrayList<>();
        for (Colllect i:list) {
            Integer productid = i.getProductid();
            QueryWrapper wrapper1 = new QueryWrapper();
            wrapper1.eq("id",productid);
            Product n = productService.getOne(wrapper1);
            ProductDto productDto = new ProductDto();
            if (n.getImage() != null && n.getImage().length()>0) {
                String[] split = n.getImage().split(",");
                productDto.setImg(Arrays.asList(split));
            }else{
                productDto.setImg(new ArrayList());
            }
            productDto.setUserid(n.getUserid());
            productDto.setContent(n.getContent());
            productDto.setColor(n.getColor());
            productDto.setBuyWay(n.getBuyWay());
            productDto.setCreateDate(n.getCreateDate());
            productDto.setDamageDegree(n.getDamageDegree());
            productDto.setId(n.getId());
            productDto.setImage(n.getImage());
            productDto.setPrice(n.getPrice());
            productDto.setTitle(n.getTitle());
            productDto.setPhone(n.getPhone());
            productDto.setOther(n.getOther());
            products.add(productDto);
        }
        return R.success(products);
    }

}
